package com.framework.bas.web.controller;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.framework.bas.bs.IPlmBasBS;
import com.framework.bas.exception.UncheckedException;
import com.framework.bas.param.GeneralParam;
import com.framework.bas.service.IControl;
import com.framework.bas.util.PlmBasConstant;
import com.framework.bas.util.SpringUtil;
import com.framework.bas.util.UtilWeb;
import com.framework.bas.web.provider.IPlmQueryProvider;
import com.framework.bas.web.provider.impl.HqlQueryProvider;
import com.framework.bas.web.provider.impl.SqlQueryProvider;

@Controller
public class AbsController implements ServletContextAware{

	private static IPlmBasBS plmBasBS = null;
	private static IControl plmBascontrol = null;
	protected ServletContext servletContext = null;
	
	protected IControl getControl() {
		if (plmBascontrol == null) {
			plmBascontrol = (IControl) SpringUtil.getBean(IControl.BEAN_ID);
		}
		return plmBascontrol;
	}
	
	protected IPlmBasBS getPlmBasBS() {
		if (plmBasBS == null) {
			plmBasBS = (IPlmBasBS) SpringUtil.getBean(IPlmBasBS.BEAN_ID);
		}
		return plmBasBS;
	}
	public HttpServletRequest getRequest() {
		return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
	}
	
	public ServletContext getServletContext() {
		return servletContext;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	/**
	 * 调用业务层Service
	 * @param param 默认已有交易码(_app_service)和登录用户(_app_user)的信息。
	 * @param context intelliWeb IActionContext or IInitContext
	 * @return
	 */
	public Object runService(GeneralParam param,HttpServletRequest request) {
		param = UtilWeb.getRequestWrap(param, request);
		return getControl().execute(param.getValueString(PlmBasConstant.APP_SERVICE), param);
	}
	
	/**
	 * 获取分页数据
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public PageSupport loadPages(String queryStatementId,String querySizeStatementId, Map param){
		IPlmQueryProvider sqlProvider = getSqlQueryProvider(queryStatementId, querySizeStatementId, param);
		return sqlProvider.queryPageData();
	}
	
	/**
	 * 获得使用iBatis查询的QueryProvider。
	 * @param queryStatementId
	 *            查询记录集的iBatis的查询Statement名称。
	 * @param querySizeStatementId
	 *            查询记录数的iBatis的查询Statement名称。
	 * @param Map
	 *            查询参数
	 * @return
	 */
	public IPlmQueryProvider getSqlQueryProvider(String queryStatementId,
			String querySizeStatementId, Map param) {
		IPlmQueryProvider sqlProvider = new SqlQueryProvider(queryStatementId, querySizeStatementId, param);
		sqlProvider.setPageSupport(UtilWeb.transJsonParam(getRequest()));
		return sqlProvider;
	}
	
	/**
	 * 获得使用Hql查询的QueryProvider。
	 * @param queryHql
	 *            查询记录集的HQL语句。
	 * @param queryHqlSize
	 *            查询记录集记录数的HQL语句。
	 * @param Map
	 *            查询参数
	 * @return
	 */
	public IPlmQueryProvider getHqlQueryProvider(String queryHql,
			String queryHqlSize, Map param) {
		IPlmQueryProvider hqlProvider = new HqlQueryProvider(queryHql, queryHqlSize, param);
		hqlProvider.setPageSupport(UtilWeb.transJsonParam(getRequest()));
		return hqlProvider;
	}
	
	@ExceptionHandler(UncheckedException.class)
	public String handleException(UncheckedException ex){
		return "redirect:/public/exception.jsp?errorCode="+ex.getErrorCode();
	}

}
